package com.hongxun.design.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.baidu.mapapi.map.offline.MKOLUpdateElement;
import com.hongxun.design.R;
import com.hongxun.design.activitys.OfflineMapActivity;
import com.hongxun.design.bean.CitysBean;
import com.hongxun.design.listeners.ListenerSet;
import com.hongxun.design.listeners.MClickListener;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.SetTypeface;
import com.hongxun.design.mytools.ShardPreferencesTool;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zerdoor_pc on 2016/7/13.
 */
public class DownloadmanagementAdapter extends BaseAdapter {
    Context context;
    ArrayList<MKOLUpdateElement> itemBeans;
    LayoutInflater inflater;

    Map<Integer, Boolean> map;

    public DownloadmanagementAdapter(Context context, ArrayList<MKOLUpdateElement> itemBeans) {
        this.context = context;

        this.itemBeans = itemBeans;
        inflater = LayoutInflater.from(context);
        map = new HashMap<>();
        for (int i = 0; i < itemBeans.size(); i++) {
            map.put(i, false);
        }
    }

    @Override
    public int getCount() {
        return itemBeans.size();
    }

    @Override
    public Object getItem(int i) {
        if (i < itemBeans.size()) {
            return itemBeans.get(i);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        if (getItem(position) == null) {
            return 1;
        } else if (((MKOLUpdateElement)getItem(position)).cityName.equalsIgnoreCase("OK")){
            return 2;
        }else {
            return 0;
        }

    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        MKOLUpdateElement bean = (MKOLUpdateElement) getItem(i);
        int viewType = getItemViewType(i);
        Holldler holldler = null;
        WifiHolldler wifiHolldler = null;
        if (view == null) {
            if (viewType == 0||viewType == 2) {
                view = inflater.inflate(R.layout.listview_item_offlinemapdownload, null);
                holldler = new Holldler(view);
                view.setTag(holldler);
            } else {
                view = inflater.inflate(R.layout.wifiupdatemap, null);
                wifiHolldler = new WifiHolldler(view);
                view.setTag(wifiHolldler);
            }
        } else {
            if (viewType == 0||viewType == 2) {
                holldler = (Holldler) view.getTag();
            } else {
                wifiHolldler = (WifiHolldler) view.getTag();
            }
        }
        try {

            if (viewType == 1) {
                wifiHolldler.title.setText("WiFi下自动更新");
                wifiHolldler.push.setChecked(ShardPreferencesTool.getshare(context, MString.WIFIUPDATE, true));
                wifiHolldler.push.setOnCheckedChangeListener(ListenerSet.founction_updatemap);
            } else if (viewType == 2) {
                if (i == 0) {
                    holldler.type.setVisibility(View.VISIBLE);
                    holldler.type.setText("下载列表");
                } else {
                    holldler.type.setVisibility(View.GONE);
                }

                holldler.name.setVisibility(View.GONE);
                holldler.size.setVisibility(View.GONE);
                holldler.button_item.setVisibility(View.GONE);
                holldler.code.setVisibility(View.GONE);
            }else  if (viewType == 0) {
                if (i == 0) {
                    holldler.type.setVisibility(View.VISIBLE);
                    holldler.type.setText("下载列表");
                } else {
                    holldler.type.setVisibility(View.GONE);
                }
                holldler.name.setText(bean.cityName);
                holldler.size.setText(formatDataSize(bean.size));
                holldler.button_item.setVisibility(View.GONE);

                if (bean.update) {
                    // "可更新";
                    setTextSize(holldler.update, 1);
                    setTextSize(holldler.delect, 1);
                } else {
                    // "最新";
                    setTextSize(holldler.update, 0);
                    setTextSize(holldler.delect, 1);
                }
                if (bean.ratio >= 0 && bean.ratio < 100 && bean.status == 3) {
                    holldler.update.setText("继续");
                    setTextSize(holldler.update, 1);
                } else {
                    holldler.update.setText("下载更新");
                    setTextSize(holldler.update, 0);
                }

                if (holldler.button_item.getVisibility() == View.VISIBLE) {
                    //显示
                    holldler.code.setText("已下载" + bean.ratio + "% ");
                    SetTypeface.setAppend(holldler.code, IconSourse.DROPTOP);
                } else {
                    //隐藏
                    holldler.code.setText("已下载" + bean.ratio + "% ");
                    SetTypeface.setAppend(holldler.code, IconSourse.DROPDOWN);
                }

                holldler.code.setOnClickListener(new MClickListener(i, bean, holldler) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        MKOLUpdateElement bean = (MKOLUpdateElement) msg;
                        Holldler holldler = (Holldler) msg2;
                        if (holldler.button_item.getVisibility() == View.VISIBLE) {
                            //隐藏
                            holldler.button_item.setVisibility(View.GONE);
                            holldler.code.setText("已下载" + bean.ratio + "% ");
                            SetTypeface.setAppend(holldler.code, IconSourse.DROPDOWN);
                            map.put(position, false);
                        } else {
                            //显示
                            holldler.button_item.setVisibility(View.VISIBLE);
                            holldler.code.setText("已下载" + bean.ratio + "% ");
                            SetTypeface.setAppend(holldler.code, IconSourse.DROPTOP);
                            map.put(position, true);
                        }


                    }
                });
                holldler.update.setOnClickListener(new MClickListener(i, bean) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        MKOLUpdateElement bean = (MKOLUpdateElement) msg;
                        ((OfflineMapActivity) context).start(bean.cityID, bean.cityName);
                    }
                });
                holldler.delect.setOnClickListener(new MClickListener(i, bean, holldler) {
                    @Override
                    public void onClick(View view, int position, Object msg) {
                        Holldler holldler = (Holldler) msg2;
                        MKOLUpdateElement bean = (MKOLUpdateElement) msg;
                        ((OfflineMapActivity) context).remove(bean.cityID, bean.cityName);
                        holldler.button_item.setVisibility(View.GONE);
                    }
                });
                try {
                    if (map.get(i)) {
                        holldler.button_item.setVisibility(View.VISIBLE);
                    } else {
                        holldler.button_item.setVisibility(View.GONE);
                    }
                } catch (Exception e) {
                    holldler.button_item.setVisibility(View.GONE);
                    map.put(i, false);
                }
            }


        } catch (Exception e) {
        }

        return view;
    }

    /**
     * 计算文件大小*
     */

    public String formatDataSize(int size) {
        String ret = "";
        if (size < (1024 * 1024)) {
            ret = String.format("%dK", size / 1024);
        } else {
            ret = String.format("%.1fM", size / (1024 * 1024.0));
        }
        return ret;
    }

    /**
     * 改变textview的字体与边框  type：0为不可选择，1为可*
     */
    private void setTextSize(TextView v, int type) {
        if (type == 0) {
            //不可选择
            v.setTextSize(16);
            v.setBackgroundResource(R.drawable.grayline3_wihte_item);
            v.setTextColor(context.getResources().getColor(R.color.gainsboro));
            v.setEnabled(false);
        } else {
            //可选择
            v.setTextSize(16);
            v.setEnabled(true);
            v.setBackgroundResource(R.drawable.blueline3_wihte_item);
            v.setTextColor(context.getResources().getColor(R.color.project_blue));
        }

    }


    /**
     * 开始下载*
     */
    private void startLoad(CitysBean bean) {
        //bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).start(bean.getCityId(), bean.getCityName());
    }

    /**
     * 继续下载*
     */
    private void continueLoad(CitysBean bean) {
        //bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).start(bean.getCityId(), bean.getCityName());
    }

    /**
     * 暂停下载*
     */
    private void stopLoad(CitysBean bean) {
        // bean.setCityCode(OfflineMapActivity.Code_continue);
        ((OfflineMapActivity) context).stop(bean.getCityId(), bean.getCityName());
    }


    /**
     * 开始更新*
     */
    private void update(CitysBean bean) {
        // bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).start(bean.getCityId(), bean.getCityName());
    }

    /**
     * 删除地图*
     */
    private void remove(CitysBean bean) {
        // bean.setCityCode(OfflineMapActivity.Code_isloading);
        ((OfflineMapActivity) context).remove(bean.getCityId(), bean.getCityName());
    }


    class Holldler {
        TextView type, name, size, code, delect, update;
        LinearLayout button_item;

        Holldler(View v) {
            type = (TextView) v.findViewById(R.id.download_type);
            name = (TextView) v.findViewById(R.id.city_name);
            size = (TextView) v.findViewById(R.id.city_size);
            code = (TextView) v.findViewById(R.id.city_code);
            delect = (TextView) v.findViewById(R.id.download_delect);
            update = (TextView) v.findViewById(R.id.download_update);
            button_item = (LinearLayout) v.findViewById(R.id.button_item);
        }
    }

    class WifiHolldler {
        TextView title;
        ToggleButton push;

        WifiHolldler(View v) {
            title = (TextView) v.findViewById(R.id.wifiupdatemap_title);
            push = (ToggleButton) v.findViewById(R.id.wifiupdatemap_push);
        }
    }

}
